#include <cstdio>
#include <algorithm>

#define MAX_RELATIVES 500


inline int get_int (int &n)
{
	return scanf ("%d", &n);
}


int street_number[MAX_RELATIVES];


int main ()
{
	int test_cases, relatives, rm, m, d, i;


	get_int (test_cases);

	while (test_cases--)
	{
		get_int (relatives);
		for (i = 0; i < relatives; ++i)
			get_int (street_number[i]);
		
		std::sort (street_number, street_number + relatives);
		rm = relatives / 2;
		m = street_number[rm]; // Mediana.

		d = 0;
		for (i = 0; i < rm; ++i)
			d += m - street_number[i];
		for (; i < relatives; ++i)
			d += street_number[i] - m;
		printf("%d\n", d);
	}

	return 0;
}